package common

import (
	"git.mycaigou.com/gfyx/common/rpc_client"
	"git.mycaigou.com/gfyx/micro-gfyx-api/http_server/backend_controller"
	"git.mycaigou.com/gfyx/micro-gfyx-api/infrastructure/common/errcode"
	"git.mycaigou.com/gfyx/micro-gfyx-api/infrastructure/dto/base"
	"git.mycaigou.com/gfyx/micro-gfyx-api/infrastructure/utils"
	"git.mycaigou.com/gfyx/micro-gfyx-api/service/base/tenant"
	"github.com/gin-gonic/gin"
)

type TenantController struct {
	backend_controller.BackendBaseController
	tenantService tenant.TenantServiceIface
}

func NewTenantController(
	tenantService tenant.TenantServiceIface,
) *TenantController {
	return &TenantController{tenantService: tenantService}
}

func (c *TenantController) RegisterRouter(g *gin.RouterGroup) {
	g.GET("/common/tenant/get-list", c.getTenantList)
}

func (c *TenantController) getTenantList(ctx *gin.Context) {
	request := &base.TenantListRequest{}
	if err := ctx.ShouldBindQuery(request); err != nil {
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, utils.GetValidateErr(request, err))
		return
	}
	list, err := c.tenantService.GetTenantList(ctx, request)
	if err != nil {
		grpcErr := rpc_client.GetGrpcError(err)
		c.Fail(ctx, grpcErr.GetCode(), grpcErr.GetMessage())
		return
	}
	c.Success(ctx, list)
}
